package com.wang.config;


import cn.hutool.extra.qrcode.QrCodeUtil;
import com.aaa.vo.AliPayVo;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.wang.vo.PayStatusVo;
import com.wang.vo.PayUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class PayConfig {
    public static String shengcheng(PayUtils payUtils) throws IOException {

        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do","2021000120615667","MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCuqAB2AKaVWaKDpJ55SiITdz0vcJ2XiOlrS5G4qsQ8D1WO5GAms6E9UKFXFsV3uh/msdz4otYQJJkPOBuadOl6iz73OMIIi0U0pD5YxKaMHXs+nW481Sd84llhgmzrU28MDHSp1nDBs8sUGvuLWnPPmHse+aU92d+6yyBS6UM7xJOwDOFxzmKPitOTkShw6quYroPiuoEcZxRB5ygadisb529PEA48xN/d/kCEOQXYISkGiqq8IVfS2sYjXt4pr6HGr95tnm92s+t3H4Qz6yLYd+/LEgP3uUObi2uDgEsmEEPS3/DcKMoITJD/PCpBJk3vwM1DNnsPmVB5goBXp/efAgMBAAECggEBAK5vYDZ6o+VZ5nOwAA6HOc1TM6qjwOFNfkMI8HsNInwRnJPgBx/gG9OPKfQ+6+2N2LqhPV6d00nAI5YNpZP33A/l7ncfGrtJzU6sExC5JtWdxC9fMHBVOuEBd99pUbseUzQnZIUThP4QwWxNHpmpNy1Izv66uowxg7+di6UDD4EqQnovJJpim5zDEzO8FG0QqxvZws4h7TPjdkVGNYMGRJBDJ1FcboBAt06r0SrPtiOVSOc/3U1tV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/3ba9heTh9/vaTVsxA73LL06Y3UV/c2MoNeBACz/G2MY3cO3DBmCeBhY+9XRK0jc/OQmsJ6VXnGNu5mA9KiEUQuwqRX8qqp4/jY7ZBA9e1V7fxg0salCNXxXwV3+aAQqwfcIinQk38fqAK8sx6hi3q9yAprJca0uNS9jY+a1dLG0Xfxl/EauB3tcTyH7X1B66q3CxwHL2Qpb188yQEGHw3PyGa2D3wJNH96EFwF25EI+5Rqk9QeZ+hqk8cs2kJzszjNE9slJCq4wIDAQAB","RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", payUtils.getOrderno());
        bizContent.put("total_amount", payUtils.getTotalmoney());
        bizContent.put("subject", "菜先生");
        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            System.out.println("response = " + response.getQrCode());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            BufferedImage image = QrCodeUtil.generate(response.getQrCode(), 300, 300);
            ImageIO.write(image, "png", byteArrayOutputStream);

            return response.getQrCode();
        }
        System.out.println("调用失败");
        return null;
    }
    public static PayStatusVo zhuangtai(String outTradeNo) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do","2021000120615667","MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCuqAB2AKaVWaKDpJ55SiITdz0vcJ2XiOlrS5G4qsQ8D1WO5GAms6E9UKFXFsV3uh/msdz4otYQJJkPOBuadOl6iz73OMIIi0U0pD5YxKaMHXs+nW481Sd84llhgmzrU28MDHSp1nDBs8sUGvuLWnPPmHse+aU92d+6yyBS6UM7xJOwDOFxzmKPitOTkShw6quYroPiuoEcZxRB5ygadisb529PEA48xN/d/kC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80jyfz1px3E0AxqdAELoQvKyaST9BeJt1hDc90gqVVSnTOxBIpuYFXhXg2rClyWDkQHywxQKBgQCQ3afX9I6pFfgTHkN2nUMWkYWuPy9MQIpgnCuXa2fOj9htFi5wlpgCzC+xFExImWKEN1p8ujDGg/gUbf7HpigxBJjOTp+XlIOTQUFxIWYUeGao+piJMYXzxCNTrgebr31Gg3aKorSf1snh0pbfoI8soX3WbddbIhIY6N9RE18SlQ==","json","GBK","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlygFiN2Cpnh1lDzu12v4R7KgW8+zoApXJTpPj11ou9IN8GTjfp8Xl9bm6FqgunKSN0o3Cece7i/3ba9heTh9/vaTVsxA73LL06Y3UV/c2MoNeBACz/G2MY3cO3DBmCeBhY+9XRK0jc/OQmsJ6VXnGNu5mA9KiEUQuwqRX8qqp4/jY7ZBA9e1V7fxg0salCNXxXwV3+aAQqwfcIinQk38fqAK8sx6hi3q9yAprJca0uNS9jY+a1dLG0Xfxl/EauB3tcTyH7X1B66q3CxwHL2Qpb188yQEGHw3PyGa2D3wJNH96EFwF25EI+5Rqk9QeZ+hqk8cs2kJzszjNE9slJCq4wIDAQAB","RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
//bizContent.put("trade_no", "2014112611001004680073956707");
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            //getSendPayDate()支付日期

            System.out.println("response.getOutTradeNo() = " + response.getOutTradeNo());
            System.out.println("response.getTradeStatus() = " + response.getTradeStatus());
            System.out.println("response.getSendPayDate() = " + response.getSendPayDate());
            System.out.println("response.getTotalAmount() = " + response.getTotalAmount());
            PayStatusVo payStatusVo = new PayStatusVo();
            payStatusVo.setOutTradeNo(response.getOutTradeNo());
            payStatusVo.setTradeStatus(response.getTradeStatus());
            payStatusVo.setSendPayDate(response.getSendPayDate());
            payStatusVo.setTotalAmount(response.getTotalAmount());
            return payStatusVo;
        } else {
            System.out.println("调用失败");
            return null;
        }

    }

}
